package cn.com.bsoft.cloud.service.serviceImpl;

import cn.com.bsoft.cloud.dao.cloud.CloudMessDao;
import cn.com.bsoft.cloud.entity.cloud.*;
import cn.com.bsoft.cloud.service.CloudMessService;
import cn.com.bsoft.cloud.utils.ResultCode;
import cn.com.bsoft.immunity.kotlin.JsonExtKt;
import cn.com.bsoft.immunity.utils.HttpUtilsKt;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import kotlin.jvm.internal.Reflection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(value="txManagerCloud")
public class CloudMessServiceImpl implements CloudMessService {

    @Autowired
    private CloudMessDao cloudMessDao;

    @Override
    public List<RouteMess> queryAllRoute() {
        return cloudMessDao.queryAllRoute();
    }

    @Override
    public List<RouteMess> findRouteByCode(String routeCode) {
        return cloudMessDao.findRouteByCode(routeCode);
    }


    @Override
    public List<FrequencyMess> queryAllFrequency() {
        return cloudMessDao.queryAllFrequency();
    }

    @Override
    public List<FrequencyMess> findFrequencyByCode(String frequencyCode) {
        return cloudMessDao.findFrequencyByCode(frequencyCode);
    }

    @Override
    public List<FormulationMess> queryAllFormulation() {
        return cloudMessDao.queryAllFormulation();
    }

    @Override
    public List<FormulationMess> findFormulationByCode(String formulationCode) {
        return cloudMessDao.findFormulationByCode(formulationCode);
    }

    @Override
    public List<DoseMess> queryAllDoses() {
        return cloudMessDao.queryAllDoses();
    }

    @Override
    public List<DoseMess> findDoseByCode(String doseCode) {
        return cloudMessDao.findDoseByCode(doseCode);
    }

    @Override
    public List<SiteMess> querySite() {
        return cloudMessDao.querySite();
    }


    @Override
    public List<SiteMess> querySiteByHoscode(String hosCode) {
        return cloudMessDao.querySiteByHoscode(hosCode);
    }

    @Override
    public List<SiteMess> querySiteByCode(String hosCode, String siteCode) {
        return cloudMessDao.querySiteByCode(hosCode, siteCode);
    }

    @Override
    public List<HospitalResult> queryHospitalMess() {
        List<HospitalResult> hosResult = new ArrayList<HospitalResult>();
        try {
            URL u = new URL(ResultCode.hospitalMess);
            Map map = new HashMap();
            List<HospitalMess> hoslist = new ArrayList<HospitalMess>();
            hoslist = HttpUtilsKt.postJson(u, map, JsonExtKt.getListJavaType(Reflection.getOrCreateKotlinClass(Object.class)));
            String json = JSON.toJSON(hoslist).toString();
            hoslist = JSONArray.parseArray(json, HospitalMess.class);
            for (HospitalMess hos:hoslist) {
                if(hos.getLevel()!=null&&hos.getLevel().equals("一级")){
                    HospitalResult hospitalResult = new HospitalResult();
                    hospitalResult.setHospitalCode(hos.getCode());
                    hospitalResult.setHospitalName(hos.getTitle());
                    hosResult.add(hospitalResult);
                }
            }
        }catch(MalformedURLException e){
            System.out.println(e.getMessage());
        }
        return hosResult;
    }

}